# coding: iso-8859-15
# Author: Marc-Andre Belanger (centvingt@videotron.ca)
# Date: 14/09/2012
#
# This module includes the gibson text texture related code

from panda3d.core import *
from direct.gui.DirectGui import *
from direct.task import Task
import random

class TextTextureFactory:
	textureArray = []
	def __init__(self, numberofbuffer):
		for i in range(numberofbuffer):
			self.textureArray.append(TextTexture())

	def getRandomTexture(self):
		num = random.randint(0, len(self.textureArray)-1)
		return self.textureArray[num]
				
class TextTexture:
	textTexture = ''
	montext='gna'
	seq = '00112233445566778899ABCDEFabcdef'
	def __init__(self):
		textSG = NodePath('')
		#bimage = PNMImage(Filename('test.png'))
		##bimage = PNMImage('test.png')
		#bimage.addAlpha()
		#bimage.alphaFill(1)
		#bimage.fill(0,0,1)
		#painter = PNMPainter(bimage)
		#painter.setPen(PNMBrush.makePixel((1,0,0,0)))
		#painter.drawLine(0,0,255,255)
		#painter.drawLine(10,10,10,245)
		#painter.drawLine(10,10,245,10)
		#painter.drawLine(245,245,10,245)
		#painter.drawLine(245,245,245,10)
		
		self.osb = OnscreenImage(image="test.png", parent=textSG)
		#self.osb = OnscreenImage(image=bimage, parent=textSG)
		self.osb.setDepthWrite(0)
		#self.osb.setImage(bimage)
		self.text = OnscreenText('X',parent=textSG,fg=(0,0,1,1),mayChange=1, pos=(-1,0.95), align=TextNode.ALeft, scale=(0.15, 0.05), wordwrap=3)
		self.text2 = OnscreenText('X',parent=textSG,fg=(0,0,1,1),mayChange=1, pos=(-0.5,0.95), align=TextNode.ALeft, scale=(0.15, 0.05), wordwrap=3)
		self.text3 = OnscreenText('X',parent=textSG,fg=(0,0,1,1),mayChange=1, pos=(0,0.95), align=TextNode.ALeft, scale=(0.15, 0.05), wordwrap=3)	
		self.text4 = OnscreenText('X',parent=textSG,fg=(0,0,1,1),mayChange=1, pos=(0.5,0.95), align=TextNode.ALeft, scale=(0.15, 0.05), wordwrap=3)

		for i in range(200):
			self.text['text'] += random.choice(self.seq)
			self.text2['text'] += random.choice(self.seq)
			self.text3['text'] += random.choice(self.seq)
			self.text4['text'] += random.choice(self.seq)
						
		self.textBuffer = base.win.makeTextureBuffer("FaceTexBuffer", 512,512)
		self.textBuffer.setActive(0)
		self.textBuffer.setSort(-1)
		self.textTexture = self.textBuffer.getTexture()
		
		textCam = base.makeCamera2d(self.textBuffer)
		textCam.reparentTo(textSG)
		taskMgr.doMethodLater(.1,self.changeText,'changeText')
		
	def changeText(self, t):
		num=random.randint(0,3)
		if num == 0:
			if len(self.text['text']) > 200:
				self.text['text'] = self.text['text'][1:len(self.text['text'])]
			self.text['text'] += random.choice(self.seq)
		elif num == 1:
			if len(self.text2['text']) > 200:
				self.text2['text'] = self.text2['text'][1:len(self.text['text'])]
			self.text2['text'] += random.choice(self.seq)
		elif num == 2:
			if len(self.text3['text']) > 200:
				self.text3['text'] = self.text3['text'][1:len(self.text['text'])]
			self.text3['text'] += random.choice(self.seq)
		elif num == 3:
			if len(self.text4['text']) > 200:
				self.text4['text'] = self.text4['text'][1:len(self.text['text'])]
			self.text4['text'] += random.choice(self.seq)
		#'%s\n%s'%(num,random.choice(string.uppercase)*num)
		self.textBuffer.setActive(1)
		base.graphicsEngine.renderFrame()
		self.textBuffer.setActive(0)
		return Task.again